﻿@namespace AntBlazor.Internal
@inherits DatePickerPanelBase
@using System.Globalization;

@{
    var calendar = CultureInfo.InvariantCulture.Calendar;

    DateTime monthFirstDayDate = new DateTime(PickerValue.Year, PickerValue.Month, 1, 0, 0, 0);
    DayOfWeek monthFirstDayOfWeek = calendar.GetDayOfWeek(monthFirstDayDate);

    int diffDay = 1 - (int)monthFirstDayOfWeek;
    DateTime startDate = monthFirstDayDate.AddDays(diffDay);
}

<div class='@(DatePicker.IsShowTime ? $"{DatePicker.PrefixCls}-datetime-panel" : "")'>
    <DatePickerTemplate PickerIndex="@PickerIndex"
                           MaxRow="6"
                           MaxCol="7"
                           ViewStartDate="startDate"
                           ShowFooter="@DatePicker.ShowToday"
                           GetRowClass="GetRowClass"
                           IsInView="date => DateHelper.IsSameMonth(date, PickerValue)"
                           IsToday="date => DateHelper.IsSameDay(date, DatePicker.CurrentDate)"
                           IsSelected="date => !IsWeek && DateHelper.IsSameDay(date, Value)"
                           GetColTitle='date => date.ToString("yyyy-MM-dd")'
                           OnRowSelect="OnRowSelect"
                           OnValueSelect="OnValueSelect"
                           GetNextColValue="date => date.AddDays(1)">
        <RenderPickerHeader>
            <DatePickerHeader PickerIndex="@PickerIndex"
                                 ShowSuperPreChange="@(!(DatePicker.IsRange && PickerIndex == 1))"
                                 ShowPreChange="@(!(DatePicker.IsRange && PickerIndex == 1))"
                                 ShowNextChange="@(!(DatePicker.IsRange && PickerIndex == 0))"
                                 ShowSuperNextChange="@(!(DatePicker.IsRange && PickerIndex == 0))"/>
        </RenderPickerHeader>
        <RenderTableHeader>
            <tr>
                @if (IsWeek)
                {
                    <th></th>
                }
                <th>一</th>
                <th>二</th>
                <th>三</th>
                <th>四</th>
                <th>五</th>
                <th>六</th>
                <th>日</th>
            </tr>
        </RenderTableHeader>
        <RenderFisrtCol>
            @if (IsWeek)
            {
                <td class="@(DatePicker.PrefixCls)-cell @(DatePicker.PrefixCls)-cell-weak">
                    @DateHelper.GetWeekOfYear(context)
                </td>
            }
        </RenderFisrtCol>
        <RenderColValue Context="currentColDate">
            @currentColDate.Day
        </RenderColValue>
    </DatePickerTemplate>

    @if (DatePicker.IsShowTime)
    {
        var startTime = new DateTime(2020, 1, 1, 0, 0, 0);

        <div class="@(DatePicker.PrefixCls)-time-panel">
            <div class="@(DatePicker.PrefixCls)-header">
                <div class="@(DatePicker.PrefixCls)-header-view">
                    @PickerValue.ToString("hh:mm:ss") <br />
                </div>
            </div>
            <div class="@(DatePicker.PrefixCls)-content">
                <ul class="@(DatePicker.PrefixCls)-time-panel-column" style="position: relative;">
                    @for (int hour = 0; hour < 24; hour++)
                    {
                        var viewTime = startTime;
                        string isSelectedCls = viewTime.Hour == Value.Hour ? $"{DatePicker.PrefixCls}-time-panel-cell-selected" : "";

                        <li class="@(DatePicker.PrefixCls)-time-panel-cell @isSelectedCls">
                            <div class="@(DatePicker.PrefixCls)-time-panel-cell-inner"
                                 @onclick="e => OnSelectHour(viewTime)">
                                @hour
                            </div>
                        </li>

                        startTime = startTime.AddHours(1);
                    }
                </ul>
                <ul class="@(DatePicker.PrefixCls)-time-panel-column" style="position: relative;">
                    @for (int minute = 0; minute < 60; minute++)
                    {
                        var viewTime = startTime;
                        string isSelectedCls = viewTime.Minute == Value.Minute ? $"{DatePicker.PrefixCls}-time-panel-cell-selected" : "";

                        <li class="@(DatePicker.PrefixCls)-time-panel-cell @isSelectedCls">
                            <div class="@(DatePicker.PrefixCls)-time-panel-cell-inner"
                                 @onclick="e => OnSelectMinute(viewTime)">
                                @minute
                            </div>
                        </li>

                        startTime = startTime.AddMinutes(1);
                    }
                </ul>
                <ul class="@(DatePicker.PrefixCls)-time-panel-column" style="position: relative;">
                    @for (int second = 0; second < 60; second++)
                    {
                        var viewTime = startTime;
                        string isSelectedCls = viewTime.Second == Value.Second ? $"{DatePicker.PrefixCls}-time-panel-cell-selected" : "";

                        <li class="@(DatePicker.PrefixCls)-time-panel-cell @isSelectedCls">
                            <div class="@(DatePicker.PrefixCls)-time-panel-cell-inner"
                                 @onclick="e => OnSelectSecond(viewTime)">
                                @second
                            </div>
                        </li>

                        startTime = startTime.AddSeconds(1);
                    }
                </ul>
            </div>
        </div>
    }
</div>

@if (DatePicker.RenderExtraFooter != null)
{
    <div class="@(DatePicker.PrefixCls)-footer">
        <div class="@(DatePicker.PrefixCls)-footer-extra">
            @DatePicker.RenderExtraFooter
        </div>
    </div>
}

@if (DatePicker.ShowToday && !DatePicker.IsRange
    && !DatePicker.IsShowTime && Picker != DatePickerType.Time)
{
    <div class="@(DatePicker.PrefixCls)-footer">
        <a class="@(DatePicker.PrefixCls)-today-btn" 
           @onclick="e => { OnSelectDate(DateTime.Now); DatePicker.Close(); }">
            今天
        </a>
    </div>
}

@if (DatePicker.IsShowTime)
{
    <div class="@(DatePicker.PrefixCls)-footer">
        <ul class="@(DatePicker.PrefixCls)-ranges">
            <li class="@(DatePicker.PrefixCls)-now">
                <a class="@(DatePicker.PrefixCls)-now-btn"
                   @onclick="e => { OnSelectTime(DateTime.Now); DatePicker.Close(); }">
                    此刻
                </a>
            </li>
            <li class="@(DatePicker.PrefixCls)-ok">
                <AntButton Type="@AntButtonType.Primary"
                           OnClick="e => DatePicker.Close()">
                    确 定
                </AntButton>
            </li>
        </ul>
    </div>
}

@code {
    [Parameter]
    public bool IsWeek { get; set; } = false;

    private string GetRowClass(DateTime viewDate)
    {
        string selectedRowClass = IsWeek && DateHelper.IsSameWeak(viewDate, Value) ? $"{DatePicker.PrefixCls}-week-panel-row-selected" : "";
        string rowClass = IsWeek ? $"{DatePicker.PrefixCls}-week-panel-row" : "";

        return $"{rowClass} {selectedRowClass}";
    }

    private void OnRowSelect(DateTime viewDate)
    {
        if (IsWeek)
        {
            OnSelectDate(viewDate);
        }
    }

    private void OnValueSelect(DateTime viewDate)
    {
        if (!IsWeek)
        {
            OnSelectDate(viewDate);
        }
    }
}